<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>疫情数据管理</el-breadcrumb-item>
      <el-breadcrumb-item>各省市</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索&添加数据 -->
      <el-row :gutter="20">
        <!-- 搜索模块开始 -->
        <el-col :span="4">
          <el-input
            placeholder="搜索城市"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="searchcncitycovList"
            @change="searchcncitycovList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchcncitycovList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 搜索模块结束 -->
        <!-- 时间选择器模块 -->
        <el-col :span="5.5">
          <el-date-picker
            v-model="datepicker"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="datepickerchange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            @clear="datepickerchange"
          >
          </el-date-picker>
        </el-col>
        <!-- 添加数据按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加数据</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格模块开始 -->
      <el-table
        v-loading="loading"
        element-loading-text="正在加载捏"
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'datetime', order: 'ascending' }"
        height="660"
      >
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="city" label="城市" width="100">
        </el-table-column>
        <el-table-column prop="datetime" sortable label="更新时间" width="200">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            {{ scope.row.datetime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="nowCount" sortable label="累计确诊">
        </el-table-column>
        <el-table-column prop="newCount" sortable label="新增确诊">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.newCount > 100" type="danger">
              {{ scope.row.newCount }}</el-tag
            >
            <el-tag
              v-if="scope.row.newCount <= 100 && scope.row.newCount > 10"
              type="warning"
            >
              {{ scope.row.newCount }}</el-tag
            >
            <el-tag v-if="scope.row.newCount <= 10 && scope.row.newCount > 0">
              {{ scope.row.newCount }}</el-tag
            >
            <el-tag v-if="scope.row.newCount == 0" type="success">
              {{ scope.row.newCount }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="deadCount" sortable label="累计死亡">
        </el-table-column>
        <el-table-column prop="newdead" sortable label="新增死亡">
        </el-table-column>
        <!-- 操作按钮列 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditcncitycovForm(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deletecncitycov(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格模块结束 -->
      <!-- 翻页插件开始 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[50, 100, 1000, 5000]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotal"
      >
      </el-pagination>
      <!-- 翻页插件结束 -->
    </el-card>
    <!-- 添加数据弹框 -->
    <el-dialog
      title="添加数据"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="addcncitycovFormClose"
    >
      <el-form
        ref="addcncitycovRef"
        :model="addcncitycovFormData"
        label-width="80px"
      >
        <el-form-item label="城市" prop="city">
          <el-input v-model="addcncitycovFormData.city"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="datetime">
          <el-date-picker
            v-model="addcncitycovFormData.datetime"
            type="date"
            placeholder="选择更新时间时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="累计确诊" prop="nowCount">
          <el-input
            v-model="addcncitycovFormData.nowCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="新增确诊" prop="newCount">
          <el-input
            v-model="addcncitycovFormData.newCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="累计死亡" prop="deadCount">
          <el-input
            v-model="addcncitycovFormData.deadCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="新增死亡" prop="newdead">
          <el-input
            v-model="addcncitycovFormData.newdead"
            type="number"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcncitycov">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑数据弹框 -->
    <el-dialog title="编辑数据" :visible.sync="editDialogVisible" width="30%">
      <el-form
        ref="editcncitycovRef"
        :model="editcncitycovFormData"
        label-width="80px"
      >
        <el-form-item label="城市" prop="city">
          <el-input v-model="editcncitycovFormData.city"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="datetime">
          <el-date-picker
            v-model="editcncitycovFormData.datetime"
            type="date"
            placeholder="选择更新时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="累计确诊" prop="nowCount">
          <el-input
            v-model="editcncitycovFormData.nowCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="新增确诊" prop="newCount">
          <el-input
            v-model="editcncitycovFormData.newCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="累计死亡" prop="deadCount">
          <el-input
            v-model="editcncitycovFormData.deadCount"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="新增死亡" prop="newdead">
          <el-input
            v-model="editcncitycovFormData.newdead"
            type="number"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editcncitycov">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表格数据
      datepicker: [],
      pickerOptions: {
        disabledDate(time) {
          let curDate = 1653667260000;
          let startDate = 1610812800000;
          return time.getTime() > curDate || time.getTime() < startDate;
        },
      },
      tableData: [
        {
          id: 101,
          city: "上海",
          newCount: 62104,
          nowCount: 4087,
          newdead: 57439,
          deadCount: 578,
          datetime: "2022-05-14T16:00:00.000Z",
        },
      ],
      addcncitycovFormData: {
        city: "上海",
        newCount: 62104,
        nowCount: 4087,
        newdead: 57439,
        deadCount: 578,
        datetime: "2022-05-14T16:00:00.000Z",
      },
      // 编辑用户数据
      editcncitycovFormData: {
        city: "上海",
        newCount: 62104,
        nowCount: 4087,
        newdead: 57439,
        deadCount: 578,
        datetime: "2022-05-14T16:00:00.000Z",
      },
      queryInfo: {
        query: "", //搜索条件
        pagenum: 1, //当前显示第几页
        pagesize: 1000, //每一页显示多少条
      },
      // 表格载入状态,如果还在加载就是true
      loading: true,
      // 总条数
      pageTotal: 0,
      //添加对话框显示隐藏
      addDialogVisible: false,
      //编辑对话框显示隐藏
      editDialogVisible: false,
    };
  },
  created() {
    this.getcncitycovList();
  },
  methods: {
    async getcncitycovList() {
      this.loading = true;
      let { data: res } = await this.$http.get("cncitycovdata", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.tableData = res.data.citydata;
        this.pageTotal = res.data.total;
      }
      this.loading = false;
    },
    searchcncitycovList() {
      this.queryInfo.pagenum = 1;
      this.getcncitycovList();
    },
    // 这两条是点击下面分页条条的函数
    handleSizeChange(params) {
      this.queryInfo.pagesize = params;
      this.getcncitycovList();
    },
    handleCurrentChange(params) {
      this.queryInfo.pagenum = params;
      this.getcncitycovList();
    },
    //展示编辑数据对话框的函数,并通过id将信息填入对话框
    async showEditcncitycovForm(id) {
      this.editDialogVisible = true;
      let { data: res } = await this.$http.get(`cncitycovdata/${id}`);
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.editcncitycovFormData = res.data;
      }
    },
    // 编辑接种信息函数
    editcncitycov() {
      this.$refs.editcncitycovRef.validate(async (valid) => {
        //向后端发送请求
        if (valid) {
          let { data: res } = await this.$http.put(
            `cncitycovdata/${this.editcncitycovFormData.id}`,
            this.editcncitycovFormData
          );
          //判断状态码
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg);
          } else {
            this.$message.success(res.meta.msg);
            this.editDialogVisible = false;
            this.getcncitycovList();
          }
        } else {
          return;
        }
      });
    },
    //添加数据的函数
    addcncitycov() {
      this.$refs.addcncitycovRef.validate(async (valid) => {
        if (valid) {
          // 向后端发送添加请求
          let { data: res } = await this.$http.post(
            "cncitycovdata/add",
            this.addcncitycovFormData
          );
          // 判断状态码
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg);
          } else {
            this.$message.success(res.meta.msg);
            this.addDialogVisible = false;
            this.getcncitycovList();
          }
        } else {
          return;
        }
      });
    },
    addcncitycovFormClose() {
      this.$refs.addcncitycovRef.resetFields();
    },
    //删除疫苗数据函数
    async deletecncitycov(id) {
      //这里开始是确认删除对话框
      let confirmResult = await this.$confirm(
        "此操作将永久本条数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
      //这里开始是向后端发送删除请求
      if (confirmResult == "confirm") {
        let { data: res } = await this.$http.delete("cncitycovdata/" + id);
        // 判断状态码
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg);
        } else {
          this.$message.success(res.meta.msg);
          this.getcncitycovList();
        }
      }
    },
    // 改变时间选择器的函数
    datepickerchange() {
      this.queryInfo.pagenum = 1;
      if (this.datepicker) {
        this.queryInfo.startdate = this.datepicker[0];
        this.queryInfo.enddate = this.datepicker[1];
      } else {
        this.queryInfo.startdate = null;
        this.queryInfo.enddate = null;
      }
      this.getcncitycovList();
    },
  },
};
</script>
<style lang="less" scoped></style>
